﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.Misc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace JiveMessenger.ViewModels
{
    public class ViewModelUpdateDownloadProgress : ViewModelBase
    {
        public RelayCommand RestartApplicationCommand { get; set; }

        private string _title;
        public string Title
        {
            get { return _title; }
            set
            {
                if (_title != value)
                {
                    Set(() => Title, ref _title, value);
                }
            }
        }

        private int _progress;
        public int Progress
        {
            get { return _progress; }
            set
            {
                if (_progress != value)
                {
                    Set(() => Progress, ref _progress, value);
                }
            }
        }

        private bool _updateDone;
        public bool UpdateDone
        {
            get { return _updateDone; }
            set
            {
                if (_updateDone != value)
                {
                    Set(() => UpdateDone, ref _updateDone, value);
                }
            }
        }

        private bool _updateSuccessful;
        public bool UpdateSuccessful
        {
            get { return _updateSuccessful; }
            set
            {
                if (_updateSuccessful != value)
                {
                    Set(() => UpdateSuccessful, ref _updateSuccessful, value);
                }
            }
        }

        private string _error;
        public string Error
        {
            get { return _error; }
            set
            {
                if (_error != value)
                {
                    Set(() => Error, ref _error, value);
                }
            }
        }



        public ViewModelUpdateDownloadProgress(string title)
        {
            this.Title = title;

            Messenger.Default.Register<NotificationMessage<int>>(this, msg =>
                {
                    switch (msg.Notification)
                    {
                        case "SetUpdateDownloadProgress":
                            this.Progress = msg.Content;
                            break;
                    }
                });
            Messenger.Default.Register<NotificationMessage<Updater.UpdateCompletedEventArgs>>(this, msg =>
                {
                    switch (msg.Notification)
                    {
                        case "UpdateCompleted":
                            UpdateDone = true;
                            if (!string.IsNullOrEmpty(msg.Content.Error))
                                Error = msg.Content.Error+"\r\nTry run the application as administrator and retry.";
                            UpdateSuccessful = msg.Content.Success;
                            break;
                    }
                });

            this.RestartApplicationCommand = new RelayCommand(RestartApplicationCommandAction);
        }

        private void RestartApplicationCommandAction()
        {
            if (!UpdateSuccessful)
            {
                Messenger.Default.Send(new NotificationMessage(this, "CloseWindow"));
                return;
            }

            String cmdLine = Environment.CommandLine;
            String workingDir = Environment.CurrentDirectory;
            //  string exeName = App.Current.GetType().Assembly.GetName().Name + ".exe";
            //    Process.Start(App.Current.GetType().Assembly.GetName().Name + ".exe");
            String cmd = Environment.ExpandEnvironmentVariables("%temp%\\JiveUpdateBatch.cmd");

            StreamWriter write = new StreamWriter(cmd);

            // write.WriteLine(installerCMD);
            write.WriteLine("timeout /t 3 /nobreak>nul");
            write.WriteLine("cd " + workingDir);
            write.WriteLine("del *.OldJiveFile");
            write.WriteLine(cmdLine);
            write.WriteLine("pause");
            write.Close();

            // start the installer helper
            Process process = new Process();
            process.StartInfo.FileName = cmd;
            process.StartInfo.Verb = "runas";
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.Start();
            // Environment.Exit(0);

            App.Current.Dispatcher.Invoke(() =>
            {
                App.Current.MainWindow.Close();
            });

        }

    }
}
